package google.android.ageofempires;

import android.graphics.Canvas;

public class AgeOfEmpiresThread extends Thread {
	private Map mMap;
	private boolean mRun = false;
	
	/*
	 * Constructor
	 */
	public AgeOfEmpiresThread(Map map) {
		mMap = map;
	}
	
	/*
	 * Gan gia tri cho Thread: true-run, false-stop
	 */
	public void setRunning(boolean run) {
		mRun = run;
	}
	
	/*
	 * Lay gia tri cua Thread: true-run, false-stop
	 */
	public boolean isRunning() {
		return mRun;
	}
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		// TODO Auto-generated method stub
		Canvas canvas = null;
		while(mRun) {
			try {
				canvas = mMap.getHolder().lockCanvas(null);
				synchronized (mMap.getHolder()) {
					mMap.onDraw(canvas);
				}				
			}
			finally {
				if(canvas != null) {
					mMap.getHolder().unlockCanvasAndPost(canvas);
				}
			}
		}
		super.run();
	}
}
